package com.t2c.tibetantochinese.utils;

import android.app.Activity;
import android.content.Context;
import android.content.res.AssetFileDescriptor;
import android.graphics.Bitmap;
import android.graphics.Typeface;
import android.media.MediaPlayer;
import android.net.Uri;
import android.support.v4.app.FragmentActivity;
import android.util.Log;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.WriterException;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.QRCodeWriter;
import com.t2c.tibetantochinese.R;
import com.t2c.tibetantochinese.home.HomeFragment;

import java.io.FileDescriptor;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by zoulu on 16/6/26.
 * 常用工具类
 */
public class CommonUtil {
    /**
     * 隐藏软键盘
     */
    public static void hideKeyboard(Activity activity) {
        InputMethodManager inputManager = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);;
        if (activity.getWindow().getAttributes().softInputMode != WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN) {
            if (activity.getCurrentFocus() != null)
                inputManager.hideSoftInputFromWindow(activity.getCurrentFocus().getWindowToken(),
                        InputMethodManager.HIDE_NOT_ALWAYS);
        }
    }

    /**
     * 显示软键盘
     */
    public static void showKeyBoard(Activity activity){
        InputMethodManager imm = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);
    }

    /**
     * 替换藏语字体
     * @param context
     * @return
     */
    public static Typeface getTibetanType(Context context){
        return Typeface.createFromAsset(context.getAssets(),
                "fonts/himalaya.ttf");
    }

    /**
     * 获取homefragment
     */
    public static HomeFragment getHomeFragment(FragmentActivity context){
        HomeFragment homeFragment;
        if(isChinese(context)) {
            homeFragment  = (HomeFragment)context.getSupportFragmentManager().findFragmentByTag(context.getResources().getString(R.string.homepage));
        }else{
            homeFragment = (HomeFragment)context.getSupportFragmentManager().findFragmentByTag(context.getResources().getString(R.string.thomepage));
        }
        return homeFragment;
    }

    public static boolean isChinese(Context context){
        return SPUtil.get(context, "LANGUAGE", "CHINESE").equals("CHINESE");
    }

    /**
     * 获取音视频播放地址
     */
    public static AssetFileDescriptor getAudioPath(Context context, String path){
        AssetFileDescriptor descriptor = null;
        path = "audio/" + path;
        try {
            descriptor = context.getAssets().openFd(path);
        } catch (IOException e) {
            e.printStackTrace();
        }
        assert descriptor != null;
        return descriptor;
    }

    /**
     * 获取视频播放地址
     */
    public static Uri getVideoPath(String url,Context context){
        String uri = "";
        switch (url){
            case "0.mp4":
                uri = "android.resource://" + context.getPackageName() + "/" + R.raw.m0;
                break;
            case "videopath001.mp4.mp4":
                uri = "android.resource://" + context.getPackageName() + "/" + R.raw.videopath001_mp4;
                break;
            case "videopath002.mp4.mp4":
                uri = "android.resource://" + context.getPackageName() + "/" + R.raw.videopath002_mp4;
                break;
            case "videopath003.mp4.mp4":
                uri = "android.resource://" + context.getPackageName() + "/" + R.raw.videopath003_mp4;
                break;
            case "videopath004.mp4.mp4":
                uri = "android.resource://" + context.getPackageName() + "/" + R.raw.videopath004_mp4;
                break;
            case "videopath005.mp4.mp4":
                uri = "android.resource://" + context.getPackageName() + "/" + R.raw.videopath005_mp4;
                break;
            case "videopath006.mp4.mp4":
                uri = "android.resource://" + context.getPackageName() + "/" + R.raw.videopath006_mp4;
                break;
            case "videopath007.mp4.mp4":
                uri = "android.resource://" + context.getPackageName() + "/" + R.raw.videopath007_mp4;
                break;
            case "videopath008.mp4.mp4":
                uri = "android.resource://" + context.getPackageName() + "/" + R.raw.videopath008_mp4;
                break;
            case "videopath009.mp4.mp4":
                uri = "android.resource://" + context.getPackageName() + "/" + R.raw.videopath009_mp4;
                break;
            case "videopath010.mp4.mp4":
                uri = "android.resource://" + context.getPackageName() + "/" + R.raw.videopath010_mp4;
                break;
            case "videopath011.mp4.mp4":
                uri = "android.resource://" + context.getPackageName() + "/" + R.raw.videopath011_mp4;
                break;
            case "videopath012.mp4.mp4":
                uri = "android.resource://" + context.getPackageName() + "/" + R.raw.videopath012_mp4;
                break;
            case "videopath014.mp4.mp4":
                uri = "android.resource://" + context.getPackageName() + "/" + R.raw.videopath014_mp4;
                break;
            case "videopath015.mp4.mp4":
                uri = "android.resource://" + context.getPackageName() + "/" + R.raw.videopath015_mp4;
                break;
            case "videopath016.mp4.mp4":
                uri = "android.resource://" + context.getPackageName() + "/" + R.raw.videopath016_mp4;
                break;
            case "videopath017.mp4.mp4":
                uri = "android.resource://" + context.getPackageName() + "/" + R.raw.videopath017_mp4;
                break;
            case "videopath018.mp4.mp4":
                uri = "android.resource://" + context.getPackageName() + "/" + R.raw.videopath018_mp4;
                break;
            case "videopath019.mp4.mp4":
                uri = "android.resource://" + context.getPackageName() + "/" + R.raw.videopath019_mp4;
                break;
            case "videopath020.mp4.mp4":
                uri = "android.resource://" + context.getPackageName() + "/" + R.raw.videopath020_mp4;
                break;
            case "videopath025.mp4.mp4":
                uri = "android.resource://" + context.getPackageName() + "/" + R.raw.videopath025_mp4;
                break;
            case "videopath026.mp4.mp4":
                uri = "android.resource://" + context.getPackageName() + "/" + R.raw.videopath026_mp4;
                break;
            case "videopath027.mp4.mp4":
                uri = "android.resource://" + context.getPackageName() + "/" + R.raw.videopath027_mp4;
                break;
            case "videopath028.mp4.mp4":
                uri = "android.resource://" + context.getPackageName() + "/" + R.raw.videopath028_mp4;
                break;
            case "videopath029.mp4.mp4":
                uri = "android.resource://" + context.getPackageName() + "/" + R.raw.videopath029_mp4;
                break;
            case "videopath030.mp4.mp4":
                uri = "android.resource://" + context.getPackageName() + "/" + R.raw.videopath030_mp4;
                break;
        }
        return Uri.parse(uri);
    }

    /**
     * zxing生成二维码
     * @param content 内容
     * @param width 长
     * @param height 宽
     * @return bitmap
     */
    public static Bitmap generateBitmap(String content, int width, int height) {
        QRCodeWriter qrCodeWriter = new QRCodeWriter();
        Map<EncodeHintType, String> hints = new HashMap<>();
        hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
        try {
            BitMatrix encode = qrCodeWriter.encode(content, BarcodeFormat.QR_CODE, width, height, hints);
            int[] pixels = new int[width * height];
            for (int i = 0; i < height; i++) {
                for (int j = 0; j < width; j++) {
                    if (encode.get(j, i)) {
                        pixels[i * width + j] = 0x00000000;
                    } else {
                        pixels[i * width + j] = 0xffffffff;
                    }
                }
            }
            return Bitmap.createBitmap(pixels, 0, width, width, height, Bitmap.Config.RGB_565);
        } catch (WriterException e) {
            e.printStackTrace();
        }
        return null;
    }

}
